package test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Area;
import entity.City;
import entity.Province;
import util.JDBCUtil;
import util.JSONUtil;

@WebServlet("/address")
public class AddressServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "selectProvince":
			selectProvince(req, resp);
			break;
		case "selectCity":
			selectCity(req, resp);
			break;
		case "selectArea":
			selectArea(req, resp);
			break;
		default:
			break;
		}
	}

	private void selectCity(HttpServletRequest req, HttpServletResponse resp) {
		String provinceId = req.getParameter("provinceId");
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<City> list = new ArrayList<City>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`city` FROM `tm_city` WHERE `province_id`=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, Integer.parseInt(provinceId));
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String ci = resultSet.getString("city");
				City city = new City(id, ci);
				list.add(city);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		JSONUtil.array2Json(list, resp);
	}
	
	private void selectArea(HttpServletRequest req, HttpServletResponse resp) {
		String cityId = req.getParameter("cityId");
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Area> list = new ArrayList<Area>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`area` FROM `tm_area` WHERE `city_id`=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, Integer.parseInt(cityId));
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String are = resultSet.getString("area");
				Area area = new Area(id, are);
				list.add(area);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		JSONUtil.array2Json(list, resp);
	}

	private void selectProvince(HttpServletRequest req, HttpServletResponse resp) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Province> list = new ArrayList<Province>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`province` FROM `tm_province`";
			preparedStatement = connection.prepareStatement(sql);
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String pro = resultSet.getString("province");
				Province province = new Province(id, pro);
				list.add(province);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		JSONUtil.array2Json(list, resp);
	}
}
